package com.leyou.search.web;



import com.leyou.common.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("init")
    public ResponseEntity<String> initData(){


        searchService.createIndexAndMapping();

        searchService.lodaData();

        return ResponseEntity.ok("索引库初始化完成");
    }

    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key){

       return searchService.getSuggestion(key);
    }

    /**
     *   搜索业务
     * @param paramDTO 请求的参数
     * @return  搜索之后的结果
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO paramDTO){

        return searchService.searchGoods(paramDTO);
    }

    @PostMapping("/filter")

    public Mono<Map<String,List<?>>> searchFilter(@RequestBody SearchParamDTO paramDTO){
        return searchService.searchFilter(paramDTO);
    }
}
